<template>
  <view>
    
    <view class="search-box">
      <!-- 搜索框吸顶 -->
      <search @click="goSearch"></search>
    </view>
    <!-- setp-1 轮播图区 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, i) in swiperList" :key="i">
        <!-- 
        <view class="swiper-item">
          <image :src="item.image_src"></image>
        </view> 
        -->
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <!-- 动态绑定图片 -->
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- setp-2 首页导航区 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!--setp-3 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"/>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧海报图 -->
          <navigator class="left-img-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{ width: item.product_list[0].image_width + 'rpx' }" mode="widthFix"/>
          </navigator> 
          <!-- 右侧海报图 右侧 4 个小图片的盒子-->
          <view class="right-img-box">
            <!-- 去掉第一张图片 -->
            <navigator class="right-img-item" v-for="(product , idx) in item.product_list" :key="idx" v-if="idx !== 0" :url="product.url">
              <image :src="product.image_src" :style="{ width: product.image_width + 'rpx' }" mode="widthFix"/>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js' // 导入自己封装的 mixin 模块
  export default {
    mixins: [badgeMix], // 将 badgeMix 混入到当前的页面中进行使用
    data() {
      return {
        swiperList:[] ,//轮播图的数据列表
        navList:[] ,//分类导航列表
        floorList: [], // 楼层的数据列表
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods:{
      async getSwiperList() {
        // 返回值解构出data,并重名为res{data:res}
        const {data} = await uni.$http.get('/api/public/v1/home/swiperdata')
        if(data.meta.status !== 200)  return uni.$showMsg()
        this.swiperList = data.message
        // uni.$showMsg('请求成功')
      },
      async getNavList() {
        const { data: res } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },
      async getFloorList() {
        const {data} = await uni.$http.get('/api/public/v1/home/floordata')
        if (data.meta.status !== 200) return uni.$showMsg()
        data.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            // 重新赋值自定义url，用于跳转页面
            prod.url = '/subpkg/goods_list/goods_list?'+ prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = data.message
      },
      navClickHandler(item) {
        console.log(item)
        if(item.name === '分类') {
          uni.switchTab({
            url:"/pages/cate/cate"
          })
        }
      },
      goSearch() {
        console.log('点击搜索...')
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  swiper {
   height: 330rpx;
  
   .swiper-item, image {
     width: 100%;
     height: 100%;
   }
  }
  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;
    .nav-item, image {
      width: 128rpx;
      height: 140rpx;
    }
  }
  .floor-title {
    // display: flex;
    width: 100%;
    height: 60rpx;
  }
  .floor-img-box {
    display: flex;
    padding-left: 10rpx;
  }
  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
  .search-box {
    position: sticky; //头部吸顶效果
    top: 0;
    z-index: 999;
  }
</style>
